/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine;

import admin.gameInterface;
import java.util.ArrayList;
import paket.Hero;

/**
 *
 * @author mete
 */
public class EngineNg {

    private int derinlik = 0;
    BestNod superDugum = null;
    int sayac = 0;
    private int gelen_deger;

    public gameInterface play(gameInterface game, int takim) {
        gelen_deger = -9999999;
        System.out.println("TAKİMMMM" + game.team);
        max(game, 0);
        System.out.println("SAYAC = " + sayac);
        System.out.println("TAKİMMMM" + game.team);
        return Hero.makeGamePad(superDugum.getHeroDizisi());
    }

    public int max(gameInterface game, int level) {
        System.out.println("MAX içerisinde level= " + level);
        Hero[][] heroes = Hero.makeHero(game);
        int ana_deger = +9999999;
        int deger;
        for (int i = 0; i < paket.BoardPanel.ROWS; i++) {
            for (int j = 0; j < paket.BoardPanel.COLS; j++) {
                if (heroes[i][j] != null) {
                    if (game.team == heroes[i][j].isTeam()) {
                        ArrayList list;
                        list = heroes[i][j].hamleleriDondur(heroes, i, j);

                        for (int f = 0; f < list.size(); f++) {

                            gameInterface klon;
                            klon = (gameInterface) list.get(f);
                            System.out.println("kkkkkkkkkkkkkkk = " + level);
                            paket.hareket hr;
                            Hero[][] klon_hero = Hero.makeHero(klon);
                            hr = paket.Hero.diffHero(heroes, klon_hero);
                            System.out.println("maxdaki team : " + klon.team);
                            Hero.savasVeHayatDus(klon_hero, klon.team, hr);
                            klon = Hero.makeGamePad(klon_hero);
                            if (Hero.KuyruguTitretenVarMi(klon_hero, 1 - klon.team)) {
                                System.out.println("Kuyruk titretilir.... Max");
                                if (level != 0) {
                                    return 1000;
                                } else {
                                    deger = 1000;
                                }
                            } else {
                                klon.team = 1 - klon.team;
                                deger = min(klon, level);
                                klon.team = 1 - klon.team;
                            }
                            if(deger>gelen_deger){
                                if (level == 0) {
                                    if (superDugum == null) {
                                        superDugum = new BestNod();
                                        superDugum.setPuan(ana_deger);
                                        superDugum.setHeroDizisi(Hero.makeHero(klon));
                                    }
                            }
                                return gelen_deger;
                            }
                            if (level == 0) {
                                if (superDugum == null) {
                                    superDugum = new BestNod();
                                    superDugum.setPuan(ana_deger);
                                    superDugum.setHeroDizisi(Hero.makeHero(klon));
                                }
                            }
                            if (deger < ana_deger) {
                                ana_deger = deger;
                                if (level == 0) {
                                    if (superDugum == null) {
                                        superDugum = new BestNod();
                                    }
                                    superDugum.setPuan(ana_deger);
                                    superDugum.setHeroDizisi(Hero.makeHero(klon));
                                }
                            }
                            if (gelen_deger != -9999999) {
                                if (ana_deger <= gelen_deger) {
                                    gelen_deger = ana_deger;
                                }
                            }

                        }

                    }

                }
            }
        }
        return ana_deger;
    }

    public int min(gameInterface game, int level) {
        sayac += 1;
        System.out.println("Min içinde level= " + level);
        Hero[][] heroes = Hero.makeHero(game);

        int maximum_deger = -99999999;
        for (int i = 0; i < paket.BoardPanel.ROWS; i++) {
            for (int j = 0; j < paket.BoardPanel.COLS; j++) {
                if (heroes[i][j] != null) {
                    if (game.team == heroes[i][j].isTeam()) {
                        ArrayList list;
                        list = heroes[i][j].hamleleriDondur(heroes, i, j);
                        for (int f = 0; f < list.size(); f++) {
                            gameInterface klon;
                            klon = (gameInterface) list.get(f);
                            int deger;
                            paket.hareket hr;
                            System.out.println("gamepad den önce team : " + klon.team);
                            Hero[][] klon_hero = Hero.makeHero(klon);
                            hr = paket.Hero.diffHero(heroes, klon_hero);
                            Hero.savasVeHayatDus(klon_hero, klon.team, hr);
                            klon = Hero.makeGamePad(klon_hero);
                            System.out.println("gamepad den sonra team : " + klon.team);
                            if (Hero.KuyruguTitretenVarMi(klon_hero, 1 - klon.team)) {
                                System.out.println("Kuyruk titretilir.... Min");
                                if (level == derinlik) {
                                    return -1000;
                                } else {
                                    deger = -1000;
                                }
                            } else {
                                if (level == derinlik) {
                                    System.out.println("team : " + (klon.team));
                                    deger = Hero.tahtaDegeri(Hero.makeHero(klon), klon.team, hr);


                                } else {
                                    klon.team = 1 - klon.team;
                                    deger = max(klon, level + 1);
                                    klon.team = 1 - klon.team;
                                }
                            }
                            if (deger > maximum_deger) {
                                maximum_deger = deger;
                            }
                            if (gelen_deger != -9999999) {
                                if (deger >= gelen_deger) {
                                    
                                    return deger;
                                }
                            }

                        }
                    }
                }
            }
        }
        if (level == derinlik && gelen_deger == -9999999) {
            gelen_deger = maximum_deger;

        }
        return maximum_deger;
    }
}
